package com.atguigu.gmall.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
@Api(tags = "spu管理接口")
public class SpuManageController {
    @Autowired
    private ManagerService manageService;

    // 根据查询条件封装控制器
    // springMVC 的时候，有个叫对象属性传值 如果页面提交过来的参数与实体类的参数一致，
    // 则可以使用实体类来接收数据
    // http://api.gmall.com/admin/product/1/10?category3Id=61
    // @RequestBody 作用 将前台传递过来的json{"category3Id":"61"}  字符串变为java 对象。
    @GetMapping("{page}/{size}")
    @ApiOperation(value = "根据三级分类id  获取 spu", notes = "根据三级分类id  获取 spu")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long size,
                                 SpuInfo spuInfo) {
        // 创建一个Page 对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, size);
        // 获取数据
        IPage<SpuInfo> spuInfoPageList = manageService.getSpuInfoPage(spuInfoPage, spuInfo);
        // 将获取到的数据返回即可！
        return Result.ok(spuInfoPageList);
    }

    // 销售属性http://api.gmall.com/admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        // 查询所有的销售属性集合
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }


    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {

        Result result = manageService.saveSpuInfo(spuInfo);
        return result;
    }

    @GetMapping("/getSpuInfo/{spuId}")
    public Result getSpuInfo(@PathVariable("spuId") Long spuId) {
        System.out.println("spuId = " + spuId);
        Result result = manageService.getSpuInfo(Long.valueOf(spuId));
        return result;
    }


     @PostMapping("/updateSpuInfo")
    public Result updateSpuInfo(@RequestBody SpuInfo spuInfo) {
        Result result = manageService.updateSpuInfo(spuInfo);
        return result;
     }






//    @GetMapping("spuImageList/{spuId}")
//    public Result getSpuImageList(@PathVariable("spuId") Long spuId){
//        Result result = manageService.getSpuImageList(spuId);
//        return result;
//    }

}
